package com.evl.api.qrcode.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.evl.api.qrcode.service.IWeiXinService;
import com.evl.api.qrcode.vo.TemplateRQ;
import com.evl.common.constant.Const;
import com.evl.common.persistence.dao.TrainOrderMapper;
import com.evl.common.persistence.dao.TrainTicketMapper;
import com.evl.common.persistence.model.FrequentContactT;
import com.evl.common.persistence.model.TrainOrder;
import com.evl.common.persistence.model.TrainTicket;
import com.evl.common.persistence.model.User;
import com.evl.config.EncoderHandler;
import com.evl.constant.ErrorContant;
import com.evl.constant.TkContant;
import com.evl.core.vo.BaseResultVo;
import com.evl.core.vo.JsonReturnHelper;
import com.evl.modular.system.dao.UserMgrDao;
import com.evl.trainOrder.service.ITrainOrderService;
import com.google.gson.JsonObject;

@Controller
@RequestMapping("/api/qrcode")
@Api( value = "检票")
public class QrCodeController {
	
	@Resource
	UserMgrDao userMgrDao;
	@Resource
	TrainOrderMapper trainOrderMapper;
	@Resource
	TrainTicketMapper  trainTicketMapper;
	@Resource
	ITrainOrderService trainOrderService;

	@Resource
	IWeiXinService iWeiXinService;

	/**
     * 检票
     */
    @ApiOperation("检票员扫描二维码")
    @RequestMapping(value = "/sendTemplateMsg", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    @ResponseBody
    public BaseResultVo sendTemplateMsg(){
    	
    	/*String param="{\"touser\": \"o2YwL0RrIVzkoEjh1zeC8nkRO7so\"," +
    			"\"template_id\": \"eg8ub507akjw7IHLZ7X-GtabLQx-uKXizDbNZUcD3wM\","+
					  "\"page\": \"index\","+         
					  "\"form_id\": \"1499342019731\","+        
					  "\"data\": {"+
					      "\"keyword1\": {"+
					          "\"value\": \"339208499\","+ 
					          "\"color\": \"#173177\""+
					          " },"+
					      "\"keyword2\": {"+
					          "\"value\": \"2015年01月05日 12:30\","+
					          "\"color\": \"#173177\""+
					          " },"+ 
					      "\"keyword3\": {"+
					          "\"value\": \"粤海喜来登酒店\","+ 
					          "\"color\": \"#173177\""+
					          "} ,"+
					      "\"keyword4\": {"+
					          "\"value\": \"广州市天河区天河路208号\","+ 
					          "\"color\": \"#173177\""+
					          " }"+
					          "},"+
					  "\"emphasis_keyword\": \"keyword1\""+
					  "}";*/
    	
    	
    	String openid="o2YwL0RrIVzkoEjh1zeC8nkRO7so";
    	String template_id="eg8ub507akjw7IHLZ7X-GtabLQx-uKXizDbNZUcD3wM";
    	String page="/page/index/index";
    	
    	JsonObject data=new JsonObject();
    	JsonObject jsonObject=new JsonObject();
    	JsonObject key1=new JsonObject();
    	 key1.addProperty("value", "339208499");
    	 key1.addProperty("color", "#173177");
    	jsonObject.add("keyword1", key1);
    	data.add("data", jsonObject);
    	
    	TemplateRQ templateRQ=new TemplateRQ();
    	templateRQ.setTouser(openid);
    	templateRQ.setTemplate_id(template_id);
    	templateRQ.setPage(page);
    	templateRQ.setData(data);
    	String res=iWeiXinService.sendTemplate(templateRQ);
    	return JsonReturnHelper.getSuccessWithData(res);
    	
    }
	
	
	 /**
     * 检票
     */
    @ApiOperation("检票员扫描二维码")
    @RequestMapping(value = "/scanQrCode", method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    @ResponseBody
    public BaseResultVo scanQrCode(
			@ApiParam(required = true, name = "ticketId", value = "车票id")
			@RequestParam(required=true) String ticketId,
    		@ApiParam(required = true, name = "openid", value = "检票员openid")
			@RequestParam(required=true) String openid) {
    	
    	//判断检票员角色
    	if(StringUtils.isNotBlank(openid)){
    		User user=userMgrDao.getByOpenId(openid);
    		if(null==user){
    			return JsonReturnHelper.getFailWithError(ErrorContant.OPEN_ID_NOT_FOUND_MSG, ErrorContant.OPEN_ID_NOT_FOUND_CODE);
    		}
    	}
    	
    	//校验票面状态
    	if(StringUtils.isNotBlank(ticketId)){
    		TrainTicket tkModel=trainTicketMapper.selectById(Long.valueOf(ticketId));
    		if(null==tkModel){
    			return JsonReturnHelper.getFailWithError(ErrorContant.TICKET_NOT_FOUND_MSG, ErrorContant.TICKET_NOT_FOUND_CODE);
    		}
    		Integer orderId=tkModel.getOrderId();
    		//校验订单状态
    		TrainOrder trainOrder=new TrainOrder();
    		trainOrder.setId(orderId);
    		TrainOrder model=trainOrderMapper.selectOne(trainOrder);
    		if(null==model){
    			return JsonReturnHelper.getFailWithError(ErrorContant.ORDER_NOT_FOUND_MSG, ErrorContant.ORDER_NOT_FOUND_CODE);
    		}
    		if(model.getOrderStatus().equals(TkContant.ORDER_STATUS_ISPAY) && tkModel.getStatus().equals(String.valueOf(Const.TRAIN_TICKET_TOBEUSED))){
	    			trainOrderService.updateById(tkModel);
    		}else{
    			return JsonReturnHelper.getFailWithError(ErrorContant.TICKET_STATUS_ERROR_MSG, ErrorContant.TICKET_STATUS_ERROR_CODE);
    		}
    	}
    	
    	return JsonReturnHelper.getSuccessWithNull();
    	
    }
    
    @ResponseBody
    @ApiOperation(value="生成二维码",httpMethod = "GET", response = FrequentContactT.class, notes = "生成二维码")
    @RequestMapping(value = "/createQrCode",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public void weixinQrcode(@ApiParam(required = true, name = "url", value = "生成二维码") @RequestParam("url") String url,  HttpServletRequest requset, HttpServletResponse response){
    	EncoderHandler encoder = new EncoderHandler();  
        encoder.encoderQRCoder(url, response); 
    }
}
